{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "13cb272e"
   },
   "source": [
    "# Quickstart with LanceDB Cloud\n",
    "\n",
    "🚀 **_If you haven’t signed up for LanceDB Cloud yet, click [here](https://cloud.lancedb.com) to get started!_**\n",
    "\n",
    "\n",
    "## Welcome to LanceDB Cloud!\n",
    "\n",
    "In this notebook, we show how to implement efficient semantic search using LanceDB Cloud. You'll learn how to:\n",
    "\n",
    "📥 Connect with LanceDB Cloud\n",
    "\n",
    "🔧 Ingest a dataset into LanceDB\n",
    "\n",
    "🚀 Build a vector index and perform semantic searches\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Li8cbkeoOxyd"
   },
   "source": [
    "## Step 1: Install LanceDB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "1CNtZ9GFOxPh"
   },
   "outputs": [],
   "source": [
    "! pip install lancedb datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Qm0a39PCPYDs"
   },
   "source": [
    "## Step 2: Obtain the API key from the [dashboard](https://cloud.lancedb.com) and Connect to LanceDB Cloud\n",
    "\n",
    "\n",
    "*  Get the `db uri`\n",
    "\n",
    "`db uri` starts with `db://`, which can be obtained from the project page on the dashboard. In the following example, `db uri` is `db://test-sfifxz`.\n",
    "\n",
    "![db-uri.png]()\n",
    "\n",
    "*  Get the `API Key`\n",
    "Obtain a LanceDB Cloud API key by clicking on the `GENERATE API KEY` from the `table` page.\n",
    "\n",
    "💡 Copy the code block for connecting to LanceDB Cloud that is shown at the last step of API key generation.\n",
    "![image.png]()\n",
    "\n",
    "*  Connect to LanceDB Cloud\n",
    "\n",
    "Copy and paste the `db uri` and the `api key` from the previous steps, or directly paste the code block for LanceDB Cloud connection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "6553603f"
   },
   "outputs": [],
   "source": [
    "uri = \"db://your-db-uri\"  # @param {type:\"string\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "36ef9c45"
   },
   "outputs": [],
   "source": [
    "api_key = \"sk_...\"  # @param {type:\"string\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "PLdKxJugXove"
   },
   "outputs": [],
   "source": [
    "import lancedb\n",
    "\n",
    "api_key = api_key\n",
    "uri = uri\n",
    "\n",
    "db = lancedb.connect(uri=uri, api_key=api_key, region=\"us-east-1\")\n",
    "\n",
    "# alternatively, you can paste the code block for LanceDB Cloud connection here\n",
    "# db = lancedb.connect(\n",
    "#   uri=\"db://your-db-uri\",\n",
    "#   api_key=\"sk_...\",\n",
    "#   region=\"us-east-1\"\n",
    "# )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8eKRYd2F7v5n"
   },
   "source": [
    "## Step 3: Ingest Data\n",
    "\n",
    "We use the `ag_news` dataset from [HuggingFace](https://huggingface.co/datasets/sunhaozhepy/ag_news_sbert_keywords_embeddings), which includes 768-dimensional precomputed embeddings. To optimize performance, we extract the first 3,000 rows from the test split for this example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "l0ezDr7suAf_"
   },
   "outputs": [],
   "source": [
    "from datasets import load_dataset\n",
    "import pyarrow as pa\n",
    "\n",
    "sample_dataset = load_dataset(\n",
    "    \"sunhaozhepy/ag_news_sbert_keywords_embeddings\", split=\"test[:3000]\"\n",
    ")\n",
    "vector_dim = len(sample_dataset[0][\"keywords_embeddings\"])\n",
    "print(sample_dataset.column_names)\n",
    "print(sample_dataset[:5])\n",
    "\n",
    "table_name = \"lancedb-cloud-quickstart\"\n",
    "table = db.create_table(table_name, data=sample_dataset, mode=\"overwrite\")\n",
    "\n",
    "# convert list to fixedsizelist on the vector column\n",
    "table.alter_columns(\n",
    "    dict(path=\"keywords_embeddings\", data_type=pa.list_(pa.float32(), vector_dim))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HJf8xZmX8VJC"
   },
   "source": [
    "ℹ️ There are various ways to specify the table schema. More details can be found in our [documentation](https://docs.lancedb.com/core/ingestion)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "udX2uY4GaOOD"
   },
   "source": [
    "## Step 4: Create a vector index\n",
    "\n",
    "We will create a vector index on the `keywords_embeddings` column.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "5aljyqpUiViE"
   },
   "outputs": [],
   "source": [
    "table.create_index(\"cosine\", vector_column_name=\"keywords_embeddings\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8oPffVhtbfRt"
   },
   "source": [
    "⚠️ WARNING: `create_index` is asynchonous so it returns when indexing is in progress. We provide the `list_indices` and `index_stats` APIs to check index status. The index name is formed by appending “_idx” to the column name. Note that `list_indices` will not return any information until the index has fully ingested and indexed all available data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "w8_SIhNrbPYw",
    "outputId": "935eaa2c-867f-4383-dcd6-d828b06df34c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⏳ Waiting for keywords_embeddings_idx to be ready...\n",
      "⏳ Waiting for keywords_embeddings_idx to be ready...\n",
      "⏳ Waiting for keywords_embeddings_idx to be ready...\n",
      "✅ keywords_embeddings_idx is ready!\n",
      "IndexStatistics(num_indexed_rows=3000, num_unindexed_rows=0, index_type='IVF_PQ', distance_type='cosine', num_indices=None)\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def wait_for_index(table, index_name):\n",
    "    POLL_INTERVAL = 10\n",
    "    while True:\n",
    "        indices = table.list_indices()\n",
    "\n",
    "        if indices and any(index.name == index_name for index in indices):\n",
    "            break\n",
    "        print(f\"⏳ Waiting for {index_name} to be ready...\")\n",
    "        time.sleep(POLL_INTERVAL)\n",
    "\n",
    "    print(f\"✅ {index_name} is ready!\")\n",
    "\n",
    "\n",
    "index_name = \"keywords_embeddings_idx\"\n",
    "wait_for_index(table, index_name)\n",
    "print(table.index_stats(index_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IiM4DJvC_2dV"
   },
   "source": [
    "## Step 5: Query\n",
    "\n",
    "Let's perform a search. Note here that only the `text`, `keywords` and `label` columns will be returned\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 224
    },
    "id": "GV77SSi-AK0v",
    "outputId": "f7fb2be3-86e8-40e7-8253-17ddfd04d948"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "toyota, profit, carmaker\n"
     ]
    },
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "summary": "{\n  \"name\": \"table\",\n  \"rows\": 5,\n  \"fields\": [\n    {\n      \"column\": \"text\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          \"Toyota to open south China plant Japan carmaker Toyota enters a joint venture to produce saloon cars in southern China.\",\n          \"Corus makes first profit as UK steel plants return to the black Corus, the Anglo-Dutch steel maker, celebrated its first ever profit yesterday and said its UK plants had contributed to the turnaround.\",\n          \"The Hunt for a Hybrid The Aug. 23 front-page article on the Toyota Prius vs. the Honda Civic implied that the main reason people prefer the Prius was its  quot;geek-chic look quot; and the image buyers want.\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"keywords\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          \"toyota, china, japan\",\n          \"corus, profit, turnaround\",\n          \"prius, civic, toyota\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"label\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0,\n        \"min\": 2,\n        \"max\": 3,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          2,\n          3\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"_distance\",\n      \"properties\": {\n        \"dtype\": \"float32\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          0.7316731214523315,\n          0.8523308038711548\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
       "type": "dataframe"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-7ecdcf08-6c53-419f-9a06-120a1f4439c0\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "      <th>keywords</th>\n",
       "      <th>label</th>\n",
       "      <th>_distance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Toyota: Some security firms promise too much I...</td>\n",
       "      <td>toyota, security, firms</td>\n",
       "      <td>3</td>\n",
       "      <td>0.624589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Toyota to open south China plant Japan carmake...</td>\n",
       "      <td>toyota, china, japan</td>\n",
       "      <td>2</td>\n",
       "      <td>0.731673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>The Hunt for a Hybrid The Aug. 23 front-page a...</td>\n",
       "      <td>prius, civic, toyota</td>\n",
       "      <td>2</td>\n",
       "      <td>0.784277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Update 8: Ford, GM Set Production Cuts on Sale...</td>\n",
       "      <td>ford, automakers, gm</td>\n",
       "      <td>2</td>\n",
       "      <td>0.796504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Corus makes first profit as UK steel plants re...</td>\n",
       "      <td>corus, profit, turnaround</td>\n",
       "      <td>2</td>\n",
       "      <td>0.852331</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-7ecdcf08-6c53-419f-9a06-120a1f4439c0')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-7ecdcf08-6c53-419f-9a06-120a1f4439c0 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-7ecdcf08-6c53-419f-9a06-120a1f4439c0');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-b4efc7e7-b7f9-4f5d-9bd6-d365e87ba396\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-b4efc7e7-b7f9-4f5d-9bd6-d365e87ba396')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-b4efc7e7-b7f9-4f5d-9bd6-d365e87ba396 button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "                                                text  \\\n",
       "0  Toyota: Some security firms promise too much I...   \n",
       "1  Toyota to open south China plant Japan carmake...   \n",
       "2  The Hunt for a Hybrid The Aug. 23 front-page a...   \n",
       "3  Update 8: Ford, GM Set Production Cuts on Sale...   \n",
       "4  Corus makes first profit as UK steel plants re...   \n",
       "\n",
       "                    keywords  label  _distance  \n",
       "0    toyota, security, firms      3   0.624589  \n",
       "1       toyota, china, japan      2   0.731673  \n",
       "2       prius, civic, toyota      2   0.784277  \n",
       "3       ford, automakers, gm      2   0.796504  \n",
       "4  corus, profit, turnaround      2   0.852331  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_dataset = load_dataset(\n",
    "    \"sunhaozhepy/ag_news_sbert_keywords_embeddings\", split=\"test[5000:5001]\"\n",
    ")\n",
    "print(query_dataset[0][\"keywords\"])\n",
    "query_embed = query_dataset[\"keywords_embeddings\"][0]\n",
    "\n",
    "table.search(query_embed).select([\"text\", \"keywords\", \"label\"]).limit(5).to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "GaouFlfbIRVG"
   },
   "source": [
    "Let's perform another search to filter by the `label` column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 224
    },
    "id": "TNDU3tSuIgeE",
    "outputId": "6c32b77a-f0cc-4af6-817a-fc02284455c0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "toyota, profit, carmaker\n"
     ]
    },
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "summary": "{\n  \"name\": \"table\",\n  \"rows\": 5,\n  \"fields\": [\n    {\n      \"column\": \"text\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          \"IT companies put to the loyalty test Cisco, IBM, Microsoft and SAP have the most loyal customers in IT, according to a report released today. The fact that they are some of the biggest, most successful IT vendors in \",\n          \"Gates: US Need Not Fear Overseas Tech The United States has nothing to fear from rapidly growing technology markets in China and India, Bill Gates, chairman and chief software architect of Microsoft Corp.\",\n          \"Does Nick Carr matter? Strategybusiness concludes that a controversial new book on the strategic value of information technology is flawed--but correct.\\\\\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"keywords\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          \"companies, loyal, loyalty\",\n          \"microsoft, china, gates\",\n          \"strategybusiness, strategic, controversial\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"label\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0,\n        \"min\": 3,\n        \"max\": 3,\n        \"num_unique_values\": 1,\n        \"samples\": [\n          3\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"_distance\",\n      \"properties\": {\n        \"dtype\": \"float32\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          0.8814901113510132\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
       "type": "dataframe"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-92f61a39-d7f1-4af1-8ea3-48cece65b4c7\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "      <th>keywords</th>\n",
       "      <th>label</th>\n",
       "      <th>_distance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Toyota: Some security firms promise too much I...</td>\n",
       "      <td>toyota, security, firms</td>\n",
       "      <td>3</td>\n",
       "      <td>0.624589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>IT companies put to the loyalty test Cisco, IB...</td>\n",
       "      <td>companies, loyal, loyalty</td>\n",
       "      <td>3</td>\n",
       "      <td>0.881490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Does Nick Carr matter? Strategybusiness conclu...</td>\n",
       "      <td>strategybusiness, strategic, controversial</td>\n",
       "      <td>3</td>\n",
       "      <td>1.033984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Siemens, Freescale Extend Auto Partnership Sie...</td>\n",
       "      <td>siemens, automotive, vdo</td>\n",
       "      <td>3</td>\n",
       "      <td>1.046048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Gates: US Need Not Fear Overseas Tech The Unit...</td>\n",
       "      <td>microsoft, china, gates</td>\n",
       "      <td>3</td>\n",
       "      <td>1.048251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-92f61a39-d7f1-4af1-8ea3-48cece65b4c7')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-92f61a39-d7f1-4af1-8ea3-48cece65b4c7 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-92f61a39-d7f1-4af1-8ea3-48cece65b4c7');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-b24c7aae-0b65-42b5-87f9-92de69d40fcf\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-b24c7aae-0b65-42b5-87f9-92de69d40fcf')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-b24c7aae-0b65-42b5-87f9-92de69d40fcf button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "                                                text  \\\n",
       "0  Toyota: Some security firms promise too much I...   \n",
       "1  IT companies put to the loyalty test Cisco, IB...   \n",
       "2  Does Nick Carr matter? Strategybusiness conclu...   \n",
       "3  Siemens, Freescale Extend Auto Partnership Sie...   \n",
       "4  Gates: US Need Not Fear Overseas Tech The Unit...   \n",
       "\n",
       "                                     keywords  label  _distance  \n",
       "0                     toyota, security, firms      3   0.624589  \n",
       "1                   companies, loyal, loyalty      3   0.881490  \n",
       "2  strategybusiness, strategic, controversial      3   1.033984  \n",
       "3                    siemens, automotive, vdo      3   1.046048  \n",
       "4                     microsoft, china, gates      3   1.048251  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(query_dataset[0][\"keywords\"])\n",
    "query_embed = query_dataset[\"keywords_embeddings\"][0]\n",
    "\n",
    "table.search(query_embed).where(\"label > 2\").select(\n",
    "    [\"text\", \"keywords\", \"label\"]\n",
    ").limit(5).to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "sZOUxfqzXr1m"
   },
   "source": [
    "## Step 6: Cleanup\n",
    "\n",
    "We can now delete the table.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "4nDltKClAhhU"
   },
   "outputs": [],
   "source": [
    "db.drop_table(table_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LCAYIFwhdviT"
   },
   "source": [
    "🎉 Congrats! You just built your first semantic search application with LanceDB Cloud!"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
